import { createSlice, PayloadAction } from '@reduxjs/toolkit';

// import { Announcement } from 'src/model/Announcement';
// import { ProcessedNotification } from 'src/model/Notification';
type Announcement = []
type ProcessedNotification = []

export type UiState = {
    workload: number;
    showSnackbar: boolean;
    snackbar: { message: string; severity?: 'notify' | 'alert' };
    notification: ProcessedNotification[];
    unreadAnnouncement: number;
    pinnedAnnouncement: Announcement[];
    demand: { base: string; amount: string } | null;
    infolist: Announcement
};

const initialState: UiState = {
    workload: 0,
    showSnackbar: false,
    snackbar: { message: '' },
    notification: [],
    unreadAnnouncement: 0,
    pinnedAnnouncement: [],
    demand: null,
    infolist: []
};

export const uiSlice = createSlice({
    name: 'ui',
    initialState,
    reducers: {
        startWaiting: (state: UiState) => {
            state.workload = state.workload + 1;
        },
        finishWaiting: (state: UiState) => {
            state.workload = state.workload - 1;
        },
        openSnackbar: (state: UiState, action: PayloadAction<UiState['snackbar']>) => {
            state.showSnackbar = true;
            state.snackbar = action.payload;
        },
        dismissSnackbar: (state: UiState) => {
            state.showSnackbar = false;
        },
        setNotification: (state: UiState, action: PayloadAction<ProcessedNotification>) => {
            state.notification = action.payload;
        },
        pushNotification: (state: UiState, action: PayloadAction<ProcessedNotification[]>) => {
            state.notification = [...action.payload, ...state.notification];
        },
        reduceNotification: (state: UiState, action: PayloadAction<number>) => {
            const temp = [...state.notification];
            temp.splice(action.payload, 1);
            state.notification = temp;
        },
        setUnreadAnnouncement: (state: UiState, action: PayloadAction<number>) => {
            state.unreadAnnouncement = action.payload;
        },
        reduceUnreadAnnouncement: (state: UiState) => {
            state.unreadAnnouncement = state.unreadAnnouncement - 1;
        },
        setPinnedAnnouncement: (state: UiState, action: PayloadAction<Announcement[]>) => {
            state.pinnedAnnouncement = action.payload;
        },
        setDemand: (state: UiState, action: PayloadAction<{ base: string; amount: string } | null>) => {
            state.demand = action.payload;
        },
    },
});

export const {
    startWaiting,
    finishWaiting,
    openSnackbar,
    dismissSnackbar,
    pushNotification,
    reduceNotification,
    setUnreadAnnouncement,
    setPinnedAnnouncement,
    reduceUnreadAnnouncement,
    setDemand,
    setNotification
} = uiSlice.actions;

export default uiSlice.reducer;
